#!/bin/sh
set -x

scriptsRoot=${LB_ROOT}
if [ -z "$scriptsRoot" ];then
    echo "missing env LB_ROOT"
    exit 1
fi

function get_asm_user {
   gridu=`ps -ef | grep pmon | grep -i +asm | awk '{print $1}'`
   if [ -z "$gridu" ]; then
      gridu=grid
   fi
   echo $gridu
}

function get_asm_sid {
     dgname=$1
     guser=$2
     dbsids=`su $guser -c "asmcmd ls $dgname"`
     if [ -z "$dbsids" ]; then
        exit 1
     fi

     for x in ${dbsids}; do
         xdgname=$dgname"/"$x
         onlineredo=`su $guser -c "asmcmd ls $xdgname | grep -i onlinelog"`
         if [ $? -eq 0 ]; then
            #find onlineredolog directory, this is not backup, but appaware folder from previous mount
            asid=`echo $x | cut -d"/" -f1`
            echo $asid
            exit 0
         fi
     done
     #did not find previously appaware mount
     exit 1
}

function get_o_asm_sid {
     dgname=$1
     guser=$2
     sidcnt=`su $guser -c "asmcmd ls $dgname|wc -l"`
     dbsids=`su $guser -c "asmcmd ls $dgname"`
     if [ -z "$dbsids" ]; then
        echo "no subdirectory found under $dgname"
        exit 1
     fi

   if [ $sidcnt -eq 1 ]; then
    asid=`echo $dbsids | cut -d"/" -f1`
    echo $asid
   else
     for x in ${dbsids}; do
         xdgname=$dgname"/"$x
         onlineredo=`su $guser -c "asmcmd ls $xdgname"`
         if [ $? -eq 0 ]; then
            #find onlineredolog directory, this is not backup, but appaware folder from previous mount
            asid=`echo $x | cut -d"/" -f1`
            if [ "$asid" != "DB_UNKNOWN" ]; then
               echo $asid
               exit 0
            fi
         fi
     done
     #did not find previously appaware mount
    fi
     exit 1
}

function get_spfile {
    dgname=$1
    dbsid=$2
    guser=$3

    spfile=`su $guser -c "asmcmd ls $dgname"/"$dbsid | grep spfile"`
    if [ -z "$spfile" ]; then
       exit 1
    fi
    echo $spfile
    exit 0
}

function get_controlfile {
    dgname=$1
    dbsid=$2
    guser=$3

    ctrlfile=`su $guser -c "asmcmd ls $dgname"/"$dbsid"/CONTROLFILE""`
    if [ -z "$ctrlfile" ]; then
       exit 1
    fi
    echo $ctrlfile
    exit 0
}

function get_grid_home {
    oratab=$1
    gridhome=` grep -v "^[#]" $oratab|grep -i "+ASM" | head -n1 | cut -f2 -d":"`
    if [ -z "$gridhome" ]; then
       exit 1
    fi
    echo $gridhome
    exit 0
}

function get_last_dgname {
    localspfile=$1
    lastdg=`grep "db_create_file_dest" $localspfile | cut -d"'" -f2 | cut -d"+" -f2`
    if [ -z "$lastdg" ]; then
       exit 1
    fi
    echo $lastdg
    exit 0
}

function get_teardown_sid {
    datadir=$1
    if [ -z "$datadir" ]; then
       exit 1
    fi
    if [ ! -d $datadir ]; then
       exit 1
    fi

    ls -ltr $datadir/init*.ora >/dev/null 2>&1
    if [ $? = 0 ]; then
       getpname=`ls -ltr $datadir/init*.ora | tail -1 | awk '{print $NF}'`
       pname=$(basename $getpname)
       pname=`echo $pname | cut -d"." -f1`
       osid=`echo $pname | cut -c5-`
       echo $osid
       return 0    
    fi

    return 1
}

function get_prev_mountpoint {
   datadir=$1

    if [ -z "$datadir" ]; then
       exit 1
    fi

    if [ ! -d $datadir ]; then
       exit 1
    fi 

    ls -ltr $datadir/pmpkeep >/dev/null 2>&1
    if [ $? = 0 ]; then
       pmp=`cat $datadir/pmpkeep`
       echo "$pmp"
       exit 0
    fi

    exit 1
}

function is_appmount_instance {
    sid=$1
    
    if [ -z "$sid" ]; then
       exit 1
    fi

    setdbfile="${scriptsRoot}/oracle/oracleclone/setdb_"$sid

    if [ ! -e "$setdbfile" ]; then
       setdbfile="${scriptsRoot}/oracle/asmclone/setdb_"$sid
       if [ ! -e "$setdbfile" ]; then
          echo "$setdbfile does not exist, not app mounted by DK"
          exit 1
       fi
    fi
    
    exit 0
}

function generate_asm_node_config {
   nodelist=$1
   user=$2
   #generate an asm_node_config file
   asmnodeconf="${scriptsRoot}/oracle/asmclone/asm_node_config.conf"
   rm $asmnodeconf  > /dev/null 2>&1
   touch $asmnodeconf
   OLDIFS=$IFS
   IFS=':'
   i=1
   localip=`ifconfig -a| grep 'inet' | awk '{print $2}' | sed 's/addr://' | head -n1`
   for n in ${nodelist}; do
       echo $n
       if [ "$n" = "$localip" ]; then
       fn=`su $user -c "hostname" | tail -1`
       else
       su $user -c "ssh -o StrictHostKeyChecking=no -l$user $n exit"
       fn=`su $user -c "ssh $user@$n hostname" | tail -1`
       fi
       echo "ASM_NODE$i:$fn" >> $asmnodeconf 
       i=$(($i + 1));  
    done
    IFS=$OLDIFS
    return 0
}

function change_own {
    user=$1
    file=$2
    #get group for the user
    grp=`id -gn $user`
    if [ -z "$grp" ]; then
       echo "failed to get group for $user"
       return 1
    fi
    chown $user:$grp $file
    chmod 755 $file
    return 0
}

function generate_catalog_str {
    archdirstr=$1
    dirs=`ls -d $archdirstr*`

    catstr="catalog start with "
    catalogstr=""
    for x in ${dirs}; do
        catalogstr=$catalogstr$catstr"'"$x"/archivelog' noprompt;"    
    done
    echo "$catalogstr"
    return 0
}

function change_dbid {
    nsid=$1
    orahome=$2
    if [ -z $nsid ]; then
       echo "new SID is not provided"
       return 1
    fi
    if [ -z $orahome ]; then
       echo "ORACLE_HOME is not provided"
       return 1
    fi
    if [ ! -d $orahome ]; then
       echo "$orahome does not exists"
       return 1
    fi
    ORACLE_SID=$nsid
    ORACLE_HOME=$orahome
    PATH=$ORACLE_HOME/bin:$PATH
    export ORACLE_SID
    export ORACLE_HOME
    export PATH

    sqlplus=$ORACLE_HOME/bin/sqlplus

$sqlplus / as sysdba << EOF
shutdown immediate;
startup mount;
exit;
EOF


    $ORACLE_HOME/bin/nid target=/ LOGFILE=nid_$nsid.log

$sqlplus / as sysdba << EOF
startup mount;
alter database open resetlogs;
exit;
EOF

    return 0
}


function verify_database {
    sid=$1
    oraclehome=$2
    checktag=$3
    oosuser=$4

    ORACLE_SID=$sid
    ORACLE_HOME=$oraclehome
    PATH=$ORACLE_HOME/bin:$PATH
    export ORACLE_SID
    export ORACLE_HOME
    export PATH

    checksql="${scriptsRoot}/oracle/oracleclone/checkstatus.sql"
    echo "set head off" > $checksql
    echo "set verify off" >> $checksql
    echo "set feed off" >> $checksql
    echo "set timing off" >> $checksql
    echo "set pages 0" >> $checksql
    echo "set echo off" >> $checksql
    echo "select open_mode from v\$database;" >> $checksql
    echo "exit;" >> $checksql

    if [ -z $oosuser ]; then
       omode=`$ORACLE_HOME/bin/sqlplus -s / as sysdba @$checksql`
    else
       omode=`su $oosuser -c "$ORACLE_HOME/bin/sqlplus -s / as sysdba @$checksql"`
    fi
    rm $checksql
    if [ "$omode" = "$checktag" ]; then
       return 0
    fi
    return 1
}

function get_first_apm_sid {
     dgname=$1
     guser=$2
     lgen=$3

     dbsids=`su $guser -c "asmcmd ls $dgname"`
     if [ -z "$dbsids" ]; then
        echo "no subdirectory found under $dgname"
        exit 1
     fi

     for x in ${dbsids}; do
         xdgname=$dgname"/"$x
         apmtag=`su $guser -c "asmcmd ls $xdgname | grep apmgen"`
         if [ $? -eq 0 ]; then
            #find appare mount generation tag
            #find generation first
            gen=`echo $apmtag | awk -F"_" '{print $(NF-1)}'`
            if [ $gen -eq $lgen ]; then
               ftag=`echo $apmtag | awk -F"_" '{print $NF}' | awk -F"/" '{print $1}'`
               if [ "$ftag" = "f" ]; then
                  asid=`echo $x | cut -d"/" -f1`
                  echo $asid
                  exit 0
               fi
            fi
         fi
     done
     #did not find previously appaware mount
     exit 1
}

function get_apm_latest_gen {
     dgname=$1
     guser=$2
     dbsids=`su $guser -c "asmcmd ls $dgname"`
     if [ -z "$dbsids" ]; then
        echo "no subdirectory found under $dgname"
        exit 1
     fi
     gen=0
     for x in ${dbsids}; do
         xdgname=$dgname"/"$x
         apmtag=`su $guser -c "asmcmd ls $xdgname | grep apmgen"`
         if [ $? -eq 0 ]; then
            #find appare mount generation tag
            tgen=`echo $apmtag | awk -F"_" '{print $(NF-1)}'`
            if [ $tgen -gt $gen ]; then
               gen=$tgen
            fi
         fi
     done
     echo $gen
     exit 0
}

